﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;

namespace VAdvantage.Print
{
    public class AmtInWords_EN : AmtInWords
    {
        /// <summary>
        /// AmtInWords_EN
        /// </summary>
        public AmtInWords_EN()
            : base()
        {
            //super ();
        }	//	AmtInWords_EN

        /** Thousands plus				*/
        private static String[] majorNames = {
		"", 
		"Thousand-", 
		"Million-",
		"Billion-", 
		"Trillion-", 
		"Quadrillion-",
		"Quintillion-"
	};

        /** Ten to Ninety				*/
        private static String[] tensNames = { 
		"", 
		"Ten", 
		"Twenty",
		"Thirty", 
		"Fourty", 
		"Fifty", 
		"Sixty", 
		"Seventy",
		"Eighty", 
		"Ninety"
	};

        /** numbers to 19				*/
        private static String[] numNames = { 
		"", 
		"One", 
		"Two",
		"Three", 
		"Four", 
		"Five", 
		"Six", 
		"Seven", 
		"Eight", 
		"Nine",
		"Ten", 
		"Eleven", 
		"Twelve", 
		"Thirteen", 
		"Fourteen", 
		"Fifteen",
		"Sixteen", 
		"Seventeen", 
		"Eighteen", 
		"Nineteen"
	};

        /// <summary>
        /// Convert Less Than One Thousand
        /// </summary>
        /// <param name="number">number</param>
        /// <returns>amt</returns>
        private String ConvertLessThanOneThousand(int number)
        {
            String soFar;
            //	Below 20
            if (number % 100 < 20)
            {
                soFar = numNames[number % 100];
                number /= 100;
            }
            else
            {
                soFar = numNames[number % 10];
                number /= 10;
                soFar = tensNames[number % 10] + soFar;
                number /= 10;
            }
            if (number == 0)
                return soFar;
            return numNames[number] + "Hundred-" + soFar;
        }	//	convertLessThanOneThousand

        /// <summary>
        /// Convert
        /// </summary>
        /// <param name="number">Number</param>
        /// <returns>amt</returns>
        private String Convert(long number)
        {
            /* special case */
            if (number == 0)
            {
                return "Zero";
            }
            String prefix = "";
            if (number < 0)
            {
                number = -number;
                prefix = "Negative ";
            }
            String soFar = "";
            int place = 0;
            do
            {
                long n = number % 1000;
                if (n != 0)
                {
                    String s = ConvertLessThanOneThousand((int)n);
                    soFar = s + majorNames[place] + soFar;
                }
                place++;
                number /= 1000;
            }
            while (number > 0);
            return (prefix + soFar).Trim();
        }	//	convert


        /// <summary>
        /// Get Amount in Words
        /// </summary>
        /// <param name="amount">numeric amount (352.80)</param>
        /// <returns>Amount</returns>
        public String GetAmtInWords(String amount)
        {

            long afterDecimal;
            if (amount == null)
                return amount;
            //
            StringBuilder sb = new StringBuilder();
            int pos = amount.LastIndexOf('.');
            int pos2 = amount.LastIndexOf(',');
            if (pos2 > pos)
                pos = pos2;
            String oldamt = amount;
            amount = amount.Replace(",", "");
            int newpos = amount.LastIndexOf('.');
            long dollars = long.Parse(amount.Substring(0, newpos));
            sb.Append(" Rs ").Append(Convert(dollars));
            for (int i = 0; i < oldamt.Length; i++)
            {
                if (pos == i) //	we are done
                {
                    String cents = oldamt.Substring(i + 1);

                    afterDecimal = long.Parse(cents);
                    cents = Convert(afterDecimal);



                    if (cents.Equals("Zero"))
                    {
                        sb.Append(" Only");
                    }
                    else
                    {
                        sb.Append(" & Paise ").Append(cents).Append(" Only");
                    }
                    break;
                }
            }
            return sb.ToString();
        }

    }


    public interface AmtInWords
    {
        /// <summary>
        ///  Get Amount in Words
        /// </summary>
        /// <param name="amount">numeric amount (352.80)</param>
        /// <returns>amount in words (three*five*two 80/100)</returns>
        String GetAmtInWords(String amount);
    }	 
}
